package hk.xing.boot.service.impl;

import hk.xing.boot.entity.Permission;
import hk.xing.boot.entity.Role;
import hk.xing.boot.mapper.PermissionMapper;
import hk.xing.boot.mapper.RoleMapper;
import hk.xing.boot.service.PermissionService;
import hk.xing.boot.vo.SysUserVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class PermissionServiceImpl implements PermissionService {



    @Resource
    private RoleMapper roleMapper;

    @Resource
    private PermissionMapper permissionMapper;

    // 获取用户所有权限
    public Set<String> getPermissionKeys(Long userId) {
        //根据用户获取对应角色列表
        SysUserVo sysUserVo = roleMapper.selectRolesByUserId(userId);
        //拿到角色id
        List<Long> roleIds = sysUserVo.getRoles().stream().map(Role::getRoleId).collect(Collectors.toList());
        //根据角色ID获取对应的权限
        return permissionMapper.selectByRoleIds(roleIds).stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }



}
